package com.pwt.controller.watch;

import com.pwt.common.CustomException;
import com.pwt.model.what.WhatCharsPo;
import com.pwt.model.what.WhatChatsHeader;
import com.pwt.model.what.WhatChatsModel;
import com.pwt.service.MERKMALCONTROLService;
import com.pwt.service.WatchChartsService;
import com.pwt.utils.CloudHomeUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@RestController
@Tag(name = "主图表")
public class ChatsController {

    @Autowired
    WatchChartsService chartsService;

    @Autowired
    MERKMALCONTROLService merkmalcontrolService;

    @Autowired
    CloudHomeUtils cloudHomeUtils;

    @PostMapping("watch/charts")
    @Operation(summary = "图表数据", description = "图表数据")
    public List<WhatChatsModel> charts(@RequestBody WhatCharsPo whatCharsPo) throws CustomException, ParseException {
        if (whatCharsPo.getStart() == null || whatCharsPo.getEnd() == null) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            whatCharsPo.setStart(simpleDateFormat.parse(simpleDateFormat.format(new Date())));
            whatCharsPo.setEnd(simpleDateFormat.parse(simpleDateFormat.format(new Date(System.currentTimeMillis() - (60 * 60 * 1000)))));
        }
        return chartsService.charts(whatCharsPo.getStart(), whatCharsPo.getEnd(), whatCharsPo.getCfgId(), whatCharsPo.getMachineIds());
    }

    @PostMapping("watch/data")
    @Operation(summary = "每秒查询一次数据", description = "每秒查询一次数据")
    public List<WhatChatsModel> queryData(@RequestBody WhatCharsPo whatCharsPo) throws CustomException, ParseException {
        if (whatCharsPo.getStart() == null || whatCharsPo.getEnd() == null) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            whatCharsPo.setStart(simpleDateFormat.parse(simpleDateFormat.format(new Date())));
            whatCharsPo.setEnd(simpleDateFormat.parse(simpleDateFormat.format(new Date(System.currentTimeMillis() - (1 * 1000)))));
        }
        return chartsService.queryData(whatCharsPo.getStart(), whatCharsPo.getEnd(), whatCharsPo.getCfgId(), whatCharsPo.getMachineIds());
    }

    @PostMapping("watch/indicator")
    @Operation(summary = "查询统计数据", description = "查询统计数据")
    public List<WhatChatsModel> queryIndicatorValues(@RequestBody WhatCharsPo whatCharsPo) throws CustomException, ParseException {
        if (whatCharsPo.getStart() == null || whatCharsPo.getEnd() == null) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            whatCharsPo.setStart(simpleDateFormat.parse(simpleDateFormat.format(new Date())));
            whatCharsPo.setEnd(simpleDateFormat.parse(simpleDateFormat.format(new Date(System.currentTimeMillis() - (60 * 60 * 1000)))));
        }
        return chartsService.queryIndicatorValues(whatCharsPo.getCfgId(), whatCharsPo.getMachineIds(), whatCharsPo.getStart(), whatCharsPo.getEnd());
    }

    @GetMapping("watch/config")
    @Operation(summary = "获取设置的图例和统计设置", description = "获取设置的图例和统计设置")
    @Parameters({
            @Parameter(name = "cfgId", description = "统计配置", required = true),
            @Parameter(name = "whatLineId", description = "产线id", required = false)
    })
    public WhatChatsHeader queryStatisticsAttribute(@RequestParam(required = true) String cfgId, @RequestParam(required = false) String whatLineId) {
        return this.chartsService.queryStatisticsAttribute(cfgId, whatLineId);
    }

    @GetMapping("update-control")
    @Operation(summary = "计算控制线", description = "计算控制线")
    public void updateControlMerkmal() {
        this.merkmalcontrolService.updateControlMerkmal();
    }


    @GetMapping("cloud-home")
    @Operation(summary = "云之家消息推送", description = "云之家消息推送")
    public String cloudHome(String jobNo) {
        String[] mes = jobNo.split(",");
        return this.cloudHomeUtils.sendMessage(Arrays.asList(mes), null);
    }

}
